//
//  HMSettingViewController.m
//  G05爱鲜蜂
//
//  Created by 小灰灰 on 16/10/15.
//  Copyright © 2016年 何源. All rights reserved.
//

#import "HMSettingViewController.h"
#import "SDImageCache.h"

static NSString *ide = @"setting";

@interface HMSettingViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,weak)UITableView *tableView;
@property (nonatomic, strong) UILabel *cacheLabel;
@property (nonatomic, strong) UILabel *existLabel;
@end

@implementation HMSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavBar];
    
    [self setTableView];
    
    self.tableView.sectionHeaderHeight = 7;
}

-(void)setNavBar{
    
    self.navigationItem.title = @"设置";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
 
    UIImage *image = [[UIImage imageNamed:@"v2_goback"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *leftBarBtnItem = [[UIBarButtonItem alloc] initWithImage:image  style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationController.navigationItem.leftBarButtonItem = leftBarBtnItem;
    
}
-(void)setTableView{
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    self.tableView = tableView;
    [self.view addSubview:tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //注册
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ide];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0){
        return 2;
    }
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
   return 7;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ide forIndexPath:indexPath];
    if(indexPath.section == 0 && indexPath.row == 0)
    {
       cell.textLabel.text = @"关于小熊";
       cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    else if(indexPath.section == 0 && indexPath.row == 1){
      cell.textLabel.text = @"清除缓存";

        UILabel *clearLabel = [[UILabel alloc] init];
        clearLabel.textColor = [UIColor grayColor];

        NSUInteger cacheNum = [[SDImageCache sharedImageCache] getSize];
        clearLabel.text =  [NSString stringWithFormat:@"%.2fM",cacheNum * 1.0 / 1024 / 1024];
        
        _cacheLabel = clearLabel;
        [cell.contentView addSubview:clearLabel];
        [clearLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(cell.contentView);
            make.width.equalTo(@60);
            make.trailing.equalTo(cell.contentView).offset(-10);
            make.height.equalTo(@20);
        }];
    }
    else{
        UILabel *exitLabel = [[UILabel alloc] init];
        exitLabel.text = @"退出当前账号";
        _existLabel = exitLabel;
        exitLabel.textAlignment = NSTextAlignmentCenter;
        [cell.contentView addSubview:exitLabel];
        [exitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(cell.contentView);
        }];
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0 && indexPath.row == 0)
    {
       //跳转小熊的github
        NSLog(@"跳转页面");
 
    }
    else if (indexPath.section == 0 && indexPath.row == 1){
      //清空缓存
        NSLog(@"清空缓存");
        [_cacheLabel removeFromSuperview];
        [_existLabel removeFromSuperview];
        [[SDImageCache sharedImageCache] clearDisk];
        [self.tableView reloadData];
    }else{
        NSLog(@"退出账号");
 
    }
}
 //隐藏状态栏
-(BOOL)prefersStatusBarHidden{
    return YES;
}

@end
